package webjoch.methods;

import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.util.Random;
import com.rsbuddy.script.wrappers.Component;
import com.rsbuddy.script.wrappers.Item;
import com.rsbuddy.script.wrappers.Widget;
import org.rsbuddy.tabs.Inventory;

import java.util.ArrayList;

/**
 * 
 * @author RSBuddy
 *
 */
public class Store {

    public static final int WIDGET = 956;
    public static final int COMPONENT_BTN_CLOSE = 18;
    public static final int COMPONENT_INVENTORY = 24;

    /**
     * Tries to buy an item. 0 is All. 1, 5 and 10 use buy 1/5/10
     * while the other numbers use buy x.
     *
     * @param itemID The id of the item.
     * @param count  The number to buy.
     * @return <tt>true</tt> on success
     */
    public static boolean buy(final int itemID, final int count) {
        if (count < 0) return false;
        if (!isOpen()) return false;
        final int inventoryCount = Inventory.getCount(true);
        Item item = getItem(itemID);
        if (item != null) {
            if (count >= 500) {
                if (item.interact("Buy 500")) {
                    Task.sleep(Random.nextInt(500, 700));
                    return buy(itemID, (count - 500));
                } else return false;
            } else if (count >= 50 && count < 500) {
                if (item.interact("Buy 50")) {
                    Task.sleep(Random.nextInt(500, 700));
                    return buy(itemID, (count - 50));
                } else return false;
            } else if (count >= 10 && count < 50) {
                if (item.interact("Buy 10")) {
                    Task.sleep(Random.nextInt(500, 700));
                    return buy(itemID, (count - 10));
                } else return false;
            } else if (count >= 5 && count < 10) {
                if (item.interact("Buy 5")) {
                    Task.sleep(Random.nextInt(500, 700));
                    return buy(itemID, (count - 5));
                } else return false;
            } else if (count >= 1 && count < 5) {
                if (item.interact("Buy 1")) {
                    Task.sleep(Random.nextInt(500, 700));
                    return buy(itemID, (count - 1));
                } else return false;
            } else {
                return Inventory.getCount(true) > inventoryCount;
            }
        }
        return false;
    }

    /**
     * Closes the store interface.
     *
     * @return <tt>true</tt> if the interface is no longer open
     */
    public static boolean close() {
        if (!isOpen()) return true;

        if (Widgets.getComponent(WIDGET, COMPONENT_BTN_CLOSE).click()) {
            Task.sleep(Random.nextInt(500, 600));
            return !isOpen();
        } else return false;
    }

    /**
     * Gets the store interface.
     *
     * @return the store <tt>Widget</tt>
     */
    public static Widget getWidget() {
        return Widgets.get(WIDGET);
    }

    /**
     * Gets the item at a given component index.
     *
     * @param index The index of the component based off of the components in the Store interface.
     * @return <tt>Component</tt> for the item at the given index; otherwise null.
     */
    public static Item getItemAt(final int index) {
        final Item[] items = getItems();
        if (items != null) {
            for (final Item item : items) {
                if (item.getComponent().getChildIndex() == index) return item;
            }
        }

        return null;
    }

    /**
     * Gets the first item found with the given id.
     *
     * @param id ID of the item to get
     * @return The <tt>Component</tt> of the item; otherwise null.
     */
    public static Item getItem(final int id) {
        final Item[] items = getItems();
        if (items != null) {
            for (final Item item : items) {
                if (item.getId() == id) return item;
            }
        }

        return null;
    }

    /**
     * Gets all the items in the store Inventory.
     *
     * @return An <tt>Component</tt> array representing all of the components in the
     *         stores <tt>Widget</tt>.
     */
    public static Item[] getItems() {
        if ((getWidget() == null) || (getWidget().getComponent(COMPONENT_INVENTORY) == null)) return null;

        ArrayList<Item> items = new ArrayList<Item>();
        Component[] components = getWidget().getComponent(COMPONENT_INVENTORY).getComponents();

        for (Component component : components) {
            if (component != null && component.getItemId() != -1) items.add(new Item(component));
        }

        return items.toArray(new Item[items.size()]);
    }

    /**
     * Returns whether or not the store interface is open.
     *
     * @return <tt>true</tt> if the store interface is open, otherwise <tt>false</tt>.
     */
    public static boolean isOpen() {
        return getWidget().isValid();
    }

}